﻿using System.Linq;
using System.Windows;
using System.Windows.Media;
using ConfApp.Models;
using ConfApp.Services;

namespace ConfApp.ViewModels
{
    public class SessionInfoViewModel : ViewModel 
    {
        private readonly Session _session;
        
        public SessionInfoViewModel(
            Session session, 
            INavigationService navigationService)
            : base(navigationService)
        {
            _session = session;
        }

        public Session Session
        {
            get { return _session; }
        }

        public int ID { get { return _session.ID; } }
        public string Title { get { return _session.Title; } }
        public string Slot { get { return _session.Slot.ToTimeSpanString(); } }
        public int Level { get { return _session.Level; } }
        public string Language { get { return _session.LanguageCode; } }
        public string Room { get { return _session.Room.Code; } }
        public string TrackCode { get { return _session.Track.Code; } }
        public string MinorInfo { get { return string.Format("{0} {1} {2} {3}",TrackCode,Level,Room,Language); } }
        public string FullInfo { get { return string.Format("{0}, {1} {2} {3} {4}", SlotInfo, TrackCode, Level, Room, Language); } }

        public string TimeInfo
        {
            get { return _session.Slot.ToTimeSpanString(); }
        }

        public string SlotInfo
        {
            get { return string.Format("{0} {1}", _session.Slot.Day.Index == 0 ? "sre." : "čet.", _session.Slot.ToTimeSpanString()); }
        }

        public Brush TrackColor { get { 
            return new SolidColorBrush(
            HandleWhiteColor(_session.Track.Color)); } }

        private Color HandleWhiteColor(Color color)
        {
            if(LightThemeEnabled)
            {
                if(color==Colors.White)
                {
                    return Colors.Black;
                }
            }
            else
            {
                if (color == Colors.Black)
                {
                    return Colors.White;
                }                
            }
            return color;
        }

        public bool LightThemeEnabled
        {
            get
            {
                return (Visibility)Application.Current.Resources["PhoneLightThemeVisibility"] == Visibility.Visible;
            }
        }

        public string Speakers
        {
            get
            {
                return _session.Speakers
                    .Select(s=>s.FullName)
                    .Aggregate("",(acc,name)=>acc+"\n"+name);
            }
        }

        public override void IsBeingActivated()
        {

        }
    }
}
